//
//  AppState.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/13.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import Foundation
import Combine

class AppState: ObservableObject {
    var settings = Settings()
    var home = Home()
    
    static func reduce(action: AppAction,
                       state: AppState) -> (AppState, AppCommand?) {
        switch action {
        case .home(let homeAction):
            return self.reduce(action: homeAction, state: state)
        case .settings(let settingsAction):
            return self.reduce(action: settingsAction, state: state)
        }
    }
    
    static func reduce(action: AppAction.Home,
                       state: AppState) -> (AppState, AppCommand?) {
        var appCommand: AppCommand?
        switch action {
        case .loadPokeMenList:
            appCommand = LoadPokeMenCommand()
        case .loadPokeMenListDone(result: let result):
            switch result {
            case .success(let model):
                state.home.list = model
                state.home.needRetry = false
            case .failure(let error):
                state.home.needRetry = true
            }
        case .expandPokemonRow(let id):
            if state.home.expandingIndex == id {
                state.home.expandingIndex = Int.max
            } else {
                state.home.expandingIndex = id
            }
        case .showPokemonPanel(model: let model):
            state.home.detailPokeMonViewModel = model
            state.home.presentPokemonPannel = true
        }
       return (state, appCommand)
    }
    
    static func reduce(action: AppAction.Settings,
                       state: AppState) -> (AppState, AppCommand?) {
        var appCommand: AppCommand?
        switch action {
        case let .login(email: email, password: password):
            state.settings.loginRequested = true
            guard !email.isEmpty, !password.isEmpty else {
                return (state, nil)
            }
            appCommand = LogonAppCommand(email: email, password: password)
        case .accountBehaviorDone(result: let result):
            state.settings.loginRequested = false
            switch result {
            case .success(let user):
                state.settings.user = user
        //        appCommand = WriteUserCommand()
            case .failure(let error):
                state.settings.settingsError = error
            }
        case .logout:
            state.settings.user = nil
        case .emailValid(let valid):
            state.settings.emailIsValid = valid
        case .registerEnable(let enable):
            state.settings.registerButtonEnable = enable
        case .accountRegister(email: let email, password: let password):
            state.settings.registerAccountRequested = true
            appCommand = RegisterCommand(email: email, password: password)
        case .accountRegisterDone(result: let result):
            state.settings.registerAccountRequested = false
            switch result {
            case .success:
                state.settings.alertRegisterDialog = true
            case .failure:
                print("failure")
            }
            print(result)
        case .clearCache:
            state.settings.user = nil
            state.home.list = nil
        }
       return (state, appCommand)
    }
}
